#ifndef THREAD_HPP
#define THREAD_HPP

#include <memory>
#include <thread>

namespace sh
{
/**
 * 线程简易封装，继承并重写run()方法即可
 *
 * @author: sherlock_lht
 */
class Thread
{
public:
    Thread() = default;
    virtual ~Thread() = default;

    /**
     * @brief   开始线程
     * @date    2021-06-04
     */
    void start()
    {
        _thread = std::unique_ptr<std::thread>(new std::thread(&Thread::run, this));
        _thread->detach();
    }

public:
    static unsigned int currentThreadId()
    {
        return pthread_self();
    }

    /**
     * Forces the current thread to sleep for secs seconds.
     */
    static void sleep(unsigned long secs)
    {
        std::this_thread::sleep_for(std::chrono::seconds(secs));
    }

    /**
     * Forces the current thread to sleep for msecs milliseconds.
     */
    static void msleep(unsigned long msecs)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
    }

    /**
     * Forces the current thread to sleep for usecs microseconds.
     */
    static void usleep(unsigned long usecs)
    {
        std::this_thread::sleep_for(std::chrono::microseconds(usecs));
    }

protected:
    /**
     * @brief   run()方法将会在线程中执行，需继承重写
     */
    virtual void run() = 0;

private:
    std::unique_ptr<std::thread> _thread;
};
}

#endif //THREAD_HPP
